///fmaa Module Driver Interface definitions
//sudo apt-get install libssl-dev

#ifndef FMAA__MODULE_INC
#define FMAA__MODULE_INC
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <cli.h>
#include <ncomms.h>
#include <usb_service.h>
#include <openssl/evp.h>

using namespace std;

class Cfmaa : public CNComms, public Cusb_service
{
public:

    Cfmaa();
    ~Cfmaa();

    int run_unit_test();
    void agent_scan();
    void agent_display_critical();
    void agent_display_error();
    void agent_display_warning();
    void agent_display_info();

    void process_critical_log();
    void process_error_log();
    void process_warn_log();
    void process_info_log();
    void process_auth_log();
    size_t fmaa_client_send_report(char *ip_address_arg, string smsg, eNetCommsMsgTypes msg_type);
    size_t fmaa_client_send_report(string smsg, eNetCommsMsgTypes msg_type);
    string system_name;
    string fma_generate_timestamp();
    string fma_server_ip_address;
    string current_time;

    char cbuff[8192];

protected:

private:

    size_t config_syslog();
    void appendToFile(string filename, string entry);
    void gather_logs();
    void TestStringOutput(void);
    void process_log_md5(string log, vector<string> *log_md5);
    string process_as_hex (const unsigned char *digest, int len);
    size_t open_fma_report_new(string filename);
    size_t open_fma_report_read(string filename);
    size_t append_fma_report(string filename);
    bool stat_file_for_changes(string filename, struct stat *filestat);
    void log_new_md5(string new_md5);
    size_t close_fma_report();

    string filename_syslog_critical;
    string filename_syslog_error;
    string filename_syslog_warning;
    string filename_syslog_info;
    string filename_syslog_auth;
    string filename_system_error_report;
    string filename_rsyslog_conf;

    string critical_log_tag;
    string error_log_tag;
    string warning_log_tag;
    string info_log_tag;

    vector<string> syslog_critical;
    vector<string> syslog_error;
    vector<string> syslog_warning;
    vector<string> syslog_info;
    vector<string> syslog_auth;
    vector<string> system_error_report;
    vector<string> syslog_file;

    vector<string> syslog_critical_productID;
    vector<string> syslog_error_productID;
    vector<string> syslog_warning_productID;
    vector<string> syslog_info_productID;
    vector<string> syslog_auth_productID;

    vector<string> syslog_critical_timestamp;
    vector<string> syslog_error_timestamp;
    vector<string> syslog_warning_timestamp;
    vector<string> syslog_info_timestamp;
    vector<string> syslog_auth_timestamp;

    vector<string> syslog_critical_report;
    vector<string> syslog_error_report;
    vector<string> syslog_warning_report;
    vector<string> syslog_info_report;
    vector<string> syslog_auth_report;

    vector<string> syslog_critical_md5;
    vector<string> syslog_error_md5;
    vector<string> syslog_warning_md5;
    vector<string> syslog_info_md5;
    vector<string> syslog_auth_md5;

    struct stat syslog_critical_stats;
    struct stat syslog_error_stats;
    struct stat syslog_warning_stats;
    struct stat syslog_info_stats;
    struct stat syslog_auth_stats;

    string ProductID;
    
    EVP_MD_CTX mdctx;
    unsigned char md_value[EVP_MAX_MD_SIZE];
    char hex_md_value[EVP_MAX_MD_SIZE*2 + 1]; //2 chars per hex byte
    unsigned int md_len;

    bool bAlreadyReported;
    bool bGenerateNewReport;
    bool bFirstTime;
    FILE *fp_system_report;
    string sresult;
    struct stat tmpstat;
    string fileintegrity_report;
    size_t error;

    int retval;
    struct sNCOMMS sNCOMMS_client_tx, sNCOMMS_client_rx;
    char buff[MAX_NCOMMS_PKT_SIZE*4];

    bool bfound_critical_tag;
    bool bfound_error_tag;
    bool bfound_warning_tag;
    bool bfound_info_tag;
};

#endif ///FMAA__MODULE_INC









